<?php
namespace Home\Controller;
use Think\Controller;
class UsersController extends Controller {
public function register()
{
	
if($_FILES["file"]["error"]==0){
	$info=$this->upload();
	$name=$info["file"]["savename"];

}else
{
	$name="default.jpg";
}

$user=D('users');
if($user->create()){
	$user->photo=$name;

	//$user->password=md5(I('post.password'));
	//$user->email=trim(I('post.email'));
	if(false !==$user->add()){
	$this->success('注册成功!','/phone/');
	}else{
	$this->error('注册失败!');
	}
}else{
	$this->error($user->getError());
	}
}
// public function register()
//     { 
//     $user = D("Users");

//         if ($user->create()) {
          
//             if ($_FILES['file']['error'] == 0) {
//                 $logoinfo = $this->upload();
//                 $pname = $logoinfo['file']['savename'];
//             } else {
//                 $pname = "default.jpg";
//             }
//             $user->telephone=trim(I('post.telephone'));
//             $user->aliasname=trim(I('post.aliasname'));
//             $password=trim(I('post.password'));
//             $user->password=md5($password);
//             $user->email=trim(I('post.email'));
//             $user->photo=$pname;
//             if (false !== $user->add()) {
//                 $this->success('注册成功！', 'login');
//             } else {         $this->error('数据错误');           }
//         } else {
//             $this->error($user->getError());
//         }
//     }
	public function upload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     'Public/Admin/photo/'; // 设置附件上传根目录  //  $upload->autoSub = false;//关闭子目录，默认true
    $upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
         $info   =   $upload->upload();  // 上传文件 
    if(!$info) {       
     $this->error($upload->getError());  
      }
else{       
	return $info;    }                   
 }
//  

    public function checkLogin(){
        $code=I('post.verify');  //获取用户在表单中输入的验证码字符串
    if(!$this->check_verify($code))$this->error('验证码错误'); //调用check_verify函数，将用户输入的验证码加密处理后和Session中已存的验证码进行比较

        $usermodel=M('users'); //实例化diuba_users表
        $map   =   array();
        $map['uname'] =trim(I('post.uname'));//
        $map['password']  =md5(trim(I('post.password')));//获取用户输入的密码，并用md5加密
       
        $res=$usermodel->where($map)->select();
        if($res==null){
            $this->error('账号或密码错误');
        }else{
        // $_SESSION['uname']=$map['uname'];
         $_SESSION['uname']=$map['uname'];
        $_SESSION['u']=$res[0]["uname"];
        // $_SESSION['alias']=$res[0]["aliasname"];
        redirect(U("Goods/goods"));
        }
        }

       public function exitLog(){
            session(null);
            redirect(U("Index/index"));
       } 

  public function verify(){
    ob_clean();//清空缓存
    $Verify = new \Think\Verify();//调用验证码类实例化对象
    $Verify->entry();//输出验证码图片，并将验证码加密处理后放到Session中
  }
  // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
   function check_verify($code, $id = ''){
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

// public function find_info(){
//      $findmodel=M('find');
//     $findlist=$findmodel->order('id asc')->select();
//     //调用试图层显示数据
//       $this->findlist=$findlist;
//        $this->display();
//     }

    //   public function lost_query(){
    //   //调用模型层获取数据
    // $lostmodel=M('lost');
    //  $lostlist=$lostmodel->order('id asc')->select();
    // //调用试图层显示数据
    //    $this->lostlist=$lostlist;
    //    $this->display();
    // }
   
   function checku(){
    $a=I('get.as');
     $userModel=M('users');
     //取出类似的用户名，最多四个
    $rs=$userModel->field("uname")->where("uname like '%$a%' ")->limit(4)->select();
    //echo $rs;
    $this->ajaxReturn($rs);
   }


}
?>